import 'package:flutter/material.dart'
    hide RefreshIndicator, RefreshIndicatorState;
import 'package:furaha_mkopo/commonTool/fm_color_tool.dart';
import 'package:intl/intl.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class FMRefreshHeader extends RefreshIndicator {

  const FMRefreshHeader({super.key}) : super(refreshStyle: RefreshStyle.Follow, height: 80.0);

  @override
  State<StatefulWidget> createState() => _FMRefreshHeaderState();
}

class _FMRefreshHeaderState extends RefreshIndicatorState<FMRefreshHeader> {


  @override
  Widget buildContent(BuildContext context, RefreshStatus mode) {

    String statusText;
    final timeText = DateFormat('MMM dd, HH:mm').format(DateTime.now());

    switch (mode) {
      case RefreshStatus.idle:
        statusText = 'Pull down to refresh';
        break;
      case RefreshStatus.refreshing:
        statusText = 'Loading...';
        break;
      case RefreshStatus.completed:
        statusText = 'Refresh completed';
        break;
      case RefreshStatus.failed:
        statusText = 'Refresh failed';
        break;
      default:
        statusText = 'Release to refresh';
    }

    return Container(
      height: widget.height,
      padding: const EdgeInsets.symmetric(vertical: 12),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          _buildIndicator(mode),
          const SizedBox(width: 10),
          Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Text(
                statusText,
                style: TextStyle(
                  fontSize: 14,
                  color: FMColorTool.color("#666666"),
                  fontWeight: FontWeight.w600,
                ),
              ),
              const SizedBox(height: 8),
              Text(
                'Last updated: $timeText',
                style: TextStyle(
                  fontSize: 14,
                  color:FMColorTool.color("#666666"),
                  fontWeight: FontWeight.w600,
                ),
              )
            ],
          )
        ],
      ),
    );
  }

  Widget _buildIndicator(RefreshStatus mode) {
    return AnimatedSwitcher(
      duration: const Duration(milliseconds: 200),
      child: mode == RefreshStatus.refreshing
          ? SizedBox(
        width: 20,
        height: 20,
        child: CircularProgressIndicator(
          strokeWidth: 2,
          color: FMColorTool.color("#666666"),
        ),
      )
          : Icon(
        Icons.arrow_downward,
        size: 25,
        color: FMColorTool.color("#666666"),
      ),
    );
  }
}